import 'package:flutter/material.dart';
import 'package:homepad/iot/basic_types.dart';
import 'package:homepad/logger/logger.dart';
import 'package:homepad/iot/light_color.dart';
import 'package:homepad/models/nova_model.dart';
import 'package:homepad/styles.dart';
import 'package:homepad/views/light_brightness_view.dart';
import 'package:homepad/views/light_color_mode_view.dart';
import 'package:homepad/views/light_color_picker_view.dart';
import 'package:homepad/views/light_color_temperature_view.dart';
import 'package:homepad/views/light_color_view.dart';
import 'package:homepad/views/panel_header.dart';
import 'package:provider/provider.dart';

class PanelLightColor extends StatelessWidget {
  final GestureTapCallback? onCancel;
  final LightColor light;

  const PanelLightColor({super.key, required this.light, this.onCancel});

  @override
  Widget build(BuildContext context) {
    final MyStyles styles = Theme.of(context).extension<MyStyles>()!;

    var lightness = context.select<NovaModel, int>(((_) => light.lightness));
    var onOff = context.select<NovaModel, bool>((_) => light.onOff);

    var hue = context.select<NovaModel, int>((nova) => light.hue);
    var saturation = context.select<NovaModel, int>((nova) => light.saturation);
    var mode = context.select<NovaModel, ColorMode>((nova) => light.mode);
    var ct = context.select<NovaModel, int>((nova) => light.ct);

    var favoriteColor = context.select<NovaModel, FavoriteColor>( (nova) => switch(mode) {
        ColorMode.colorTemperature => FavoriteColor.colorTemperature(colorTemperature: ct),
        ColorMode.hueAndSaturation => FavoriteColor.hueSaturation(hue: hue, saturation: saturation),
    });

    final backgroundDecoration =
        BoxDecoration(borderRadius: styles.radius, color: styles.panelBackgroundColor);
    final foregroundDecoration =
        backgroundDecoration.copyWith(color: styles.panelDisableForegroundColor);

    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          width: styles.panelWidgetWidth,
          height: styles.panelWidgetHeight,
          decoration: backgroundDecoration,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              PanelServiceHeader(node: light),
              const Spacer(),
              LightBrightnessView(
                  min: 0,
                  max: 100,
                  step: 1,
                  current: onOff ? lightness.toDouble() : 0,
                  onChanged: (value) {
                    light.setLightness(value.toInt());
                  }),
              const Spacer(),
            ],
          ),
        ),
        SizedBox(width: styles.panelWidgetSpace),
        Container(
          width: styles.panelWidgetHeight,
          height: styles.panelWidgetHeight,
          decoration: backgroundDecoration,
          foregroundDecoration: onOff ? null : foregroundDecoration,
          child: Stack(
            children: [
              Positioned(
                top: 20,
                left: 20,
                child: Text(mode.name,
                    style: styles.titleStyle.copyWith(
                      letterSpacing: 0.45,
                      fontSize: 22,
                    )),
              ),
              if (mode == ColorMode.colorTemperature)
                Positioned(
                    top: 22,
                    left: 73,
                    child: Text("${ct}k",
                        style: styles.textStyle.copyWith(
                          letterSpacing: 0.45,
                          fontSize: 20,
                        ))),
              if (mode == ColorMode.colorTemperature)
                Padding(
                    padding: EdgeInsets.only(
                      left: (styles.panelWidgetHeight - 440) / 2,
                      top: 140 - 20,
                    ),
                    child: LightColorTemperatureView(
                      enable: onOff,
                      ct: ct,
                      onChanged: (value) {
                        light.setColorTemperature(value);
                      },
                    )),
              if (mode == ColorMode.hueAndSaturation &&
                  light.isSupported(ColorCapability.hueSaturation))
                Padding(
                    padding: EdgeInsets.only(
                      left: (styles.panelWidgetHeight - 400) / 2,
                      top: 15,
                    ),
                    child: LightColorView(
                      hue: hue,
                      saturation: saturation,
                      enable: onOff,
                      onChanged: (({int hue, int saturation}) color) {
                        logger.info("color update te: hs(${color.hue} ${color.saturation})");
                        light.setHue(color.hue, saturation: color.saturation);
                      },
                    )),
              Positioned(
                left: 45,
                right: 45,
                top: 395,
                child: LightColorPickerView(
                  color: favoriteColor,
                  favorites: light.favorites,
                  enable: onOff,
                  onChanged: light.setFavoriteColor,
                )
              ),
              if (light.isSupported(ColorCapability.hueSaturation))
                Positioned(
                  top: 15,
                  right: 15,
                  child:
                      LightColorModeView(enable: onOff, mode: mode, onChanged: light.setColorMode),
                ),
            ],
          ),
        )
      ],
    );
  }
}
